package com.ruoyi.demo.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.demo.mapper.AuctionGoodsMapper;
import com.ruoyi.demo.domain.AuctionGoods;
import com.ruoyi.demo.domain.AuctionSessionGoods;
import com.ruoyi.demo.domain.dto.AuctionGoodsDetailDTO;
import com.ruoyi.demo.service.IAuctionGoodsService;
import com.ruoyi.demo.service.IAuctionCategoryService;
import com.ruoyi.demo.service.IAuctionUserService;
import com.ruoyi.demo.service.IAuctionSessionGoodsService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

/**
 * 商品信息Service业务层处理
 * 
 * @author metaee
 * @date 2025-02-24
 */
@Service
public class AuctionGoodsServiceImpl implements IAuctionGoodsService 
{
    @Autowired
    private AuctionGoodsMapper auctionGoodsMapper;

    @Autowired
    private IAuctionCategoryService categoryService;
    
    @Autowired
    private IAuctionUserService userService;

    @Autowired
    private IAuctionSessionGoodsService sessionGoodsService;

    /**
     * 查询商品信息
     * 
     * @param goodsId 商品信息主键
     * @return 商品信息
     */
    @Override
    public AuctionGoods selectAuctionGoodsByGoodsId(Long goodsId)
    {
        return auctionGoodsMapper.selectAuctionGoodsByGoodsId(goodsId);
    }

    /**
     * 查询商品信息列表
     * 
     * @param auctionGoods 商品信息
     * @return 商品信息
     */
    @Override
    public List<AuctionGoods> selectAuctionGoodsList(AuctionGoods auctionGoods)
    {
        return auctionGoodsMapper.selectAuctionGoodsList(auctionGoods);
    }

    /**
     * 新增商品信息
     * 
     * @param auctionGoods 商品信息
     * @return 结果
     */
    @Override
    public int insertAuctionGoods(AuctionGoods auctionGoods)
    {
        auctionGoods.setCreateTime(DateUtils.getNowDate());
        return auctionGoodsMapper.insertAuctionGoods(auctionGoods);
    }

    /**
     * 修改商品信息
     * 
     * @param auctionGoods 商品信息
     * @return 结果
     */
    @Override
    public int updateAuctionGoods(AuctionGoods auctionGoods)
    {
        auctionGoods.setUpdateTime(DateUtils.getNowDate());
        return auctionGoodsMapper.updateAuctionGoods(auctionGoods);
    }

    /**
     * 批量删除商品信息
     * 
     * @param goodsIds 需要删除的商品信息主键
     * @return 结果
     */
    @Override
    public int deleteAuctionGoodsByGoodsIds(Long[] goodsIds)
    {
        return auctionGoodsMapper.deleteAuctionGoodsByGoodsIds(goodsIds);
    }

    /**
     * 删除商品信息信息
     * 
     * @param goodsId 商品信息主键
     * @return 结果
     */
    @Override
    public int deleteAuctionGoodsByGoodsId(Long goodsId)
    {
        return auctionGoodsMapper.deleteAuctionGoodsByGoodsId(goodsId);
    }

    /**
     * 查询商品详细信息，包括分类和用户信息
     * 
     * @param goodsId 商品信息主键
     * @return 商品详细信息
     */
    @Override
    public AuctionGoodsDetailDTO selectAuctionGoodsDetailByGoodsId(Long goodsId)
    {
        // 查询商品基本信息
        AuctionGoods auctionGoods = auctionGoodsMapper.selectAuctionGoodsByGoodsId(goodsId);
        if (auctionGoods == null) {
            return null;
        }
        
        // 创建DTO对象并复制属性
        AuctionGoodsDetailDTO detailDTO = new AuctionGoodsDetailDTO();
        BeanUtils.copyProperties(auctionGoods, detailDTO);
        
        // 查询分类信息
        if (auctionGoods.getCategoryId() != null) {
            detailDTO.setCategory(categoryService.selectAuctionCategoryByCategoryId(auctionGoods.getCategoryId()));
        }
        
        // 查询用户信息
        if (auctionGoods.getUserId() != null) {
            detailDTO.setUser(userService.selectAuctionUserByUserId(auctionGoods.getUserId()));
        }

        // 查询专场商品关联信息
        LambdaQueryWrapper<AuctionSessionGoods> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AuctionSessionGoods::getGoodsId, goodsId)
                   .eq(AuctionSessionGoods::getStatus, "0"); // 状态为正常的关联
        AuctionSessionGoods sessionGoods = sessionGoodsService.getOne(queryWrapper);
        
        if (sessionGoods != null) {
            detailDTO.setSessionId(sessionGoods.getSessionId());
            detailDTO.setIsSessionGoods(true);
        } else {
            detailDTO.setIsSessionGoods(false);
        }
        
        return detailDTO;
    }
}
